﻿using System;
using System.Runtime.Serialization;
using DBFile.Core.Interfaces;

namespace DBFile.Core.Impl
{
    [System.Runtime.Serialization.DataContractAttribute(Name = "DBFileInfo", Namespace = "http://schemas.datacontract.org/2004/07/DBFile.Core.Impl")]
    [Serializable]
    public class DBFileInfo : IDBFile
    {
        public DBFileInfo()
        { }

        public DBFileInfo(IDBFile dbFile)
        {
            Populate(dbFile, this);
        }

        [DataMember()]
        public long ContentLength { get; set; }

        [DataMember()]
        public string FileName { get; set; }

        [DataMember()]
        public string ContentType { get; set; }

        [DataMember()]
        public byte[] FileBytes { get; set; }

        public T ConvertTo<T>() where T : IDBFile, new()
        {
            T covertOut = new T();

            Populate(this, covertOut);

            return covertOut;
        }

        private static void Populate(IDBFile inObject, IDBFile objectToPopulate)
        {
            objectToPopulate.ContentType = inObject.ContentType;
            objectToPopulate.FileName = inObject.FileName;
            objectToPopulate.ContentLength = inObject.ContentLength;
            int length = (int)inObject.ContentLength;

            objectToPopulate.FileBytes = new byte[length];
            Array.Copy(inObject.FileBytes, objectToPopulate.FileBytes, length);
        }
    }
}
